package com.ruoyi.client.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.client.pojo.form.AddressInsertForm;
import com.ruoyi.client.pojo.form.AddressUpdateForm;
import com.ruoyi.client.pojo.vo.AddressVo;
import com.ruoyi.client.service.AddressService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Anonymous
@CrossOrigin
@RestController("ClientAddressController")
@Api(value = "v1", tags = "06.地址模块")
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressService addressService;

    /**
     * 增加地址
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "增加地址", notes = "增加我的新的收货地址")
    @ApiImplicitParam(name = "form", value = "增加地址参数", dataType = "AddressInsertForm", required = true)
    @PostMapping("/insert")
    public R<Void> insert(@RequestBody AddressInsertForm form) {
        return addressService.insert(form) ? R.ok() : R.fail();
    }

    /**
     * 删除收货地址
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "删除收货地址", notes = "删除我的收货地址，参数为地址id")
    @ApiImplicitParam(name = "id", value = "地址id", dataType = "Long", required = true)
    @DeleteMapping("/{id}")
    public R<Void> deleteById(@PathVariable("id") Long id) {
        return addressService.deleteById(id) ? R.ok() : R.fail();
    }

    /**
     * 更改地址
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "更改地址", notes = "更改我的收货地址")
    @ApiImplicitParam(name = "form", value = "更改地址时参数", dataType = "AddressUpdateForm", required = true)
    @PostMapping("/update")
    public R<Void> update(@RequestBody AddressUpdateForm form) {
        return addressService.update(form) ? R.ok() : R.fail();
    }

    /**
     * 查询收货地址详情 ById
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "查询收货地址详情", notes = "查询我的收货地址详情，参数为地址id")
    @ApiImplicitParam(name = "id", value = "地址id", dataType = "Long", required = true)
    @GetMapping("/{id}")
    public R<AddressVo> detailById(@PathVariable("id") Long id) {
        AddressVo addressVo = addressService.selectDetailById(id);
        return R.ok(addressVo);
    }

    /**
     * 查询默认收货地址
     */
    @ApiOperationSupport(order = 31)
    @ApiOperation(value = "查询默认收货地址", notes = "查询我的默认收货地址，如果传递了id就查询id对应的收货地址，如果没有传递id就查询默认的收货地址")
    @ApiImplicitParam(name = "addressId", value = "地址id", dataType = "Long", required = false)
    @GetMapping("/default")
    public R<AddressVo> getDefaultAddress(Long addressId) {
        AddressVo addressVo = addressService.getDefaultAddress(addressId);
        return R.ok(addressVo);
    }

    /**
     * 查询我的收货地址列表
     */
    @ApiOperationSupport(order = 32)
    @ApiOperation(value = "我的收货地址列表", notes = "返回我的收货地址列表")
    @GetMapping("/list")
    public R<List<AddressVo>> addressList() {
        return R.ok(addressService.selectListByMy());
    }
}